package com.example.stray_animal_management_system.admin.Statistics;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.stray_animal_management_system.commonu.service.AdoptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.stray_animal_management_system.entity.Adoption;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/7
 */
@RestController
@RequestMapping("/api/admin/statistics")
public class AdoptionStatisticsController {

    @Autowired
    private AdoptionService adoptionService;

    /**
     * 获取所有领养申请数量
     */
    @GetMapping("/adoptionCount")
    public Map<String, Object> getAdoptionCount() {
        Map<String, Object> res = new HashMap<>();
        long count = adoptionService.count();  // 获取所有领养申请数
        res.put("code", 200);
        res.put("data", count);
        return res;
    }

    /**
     * 按状态统计领养申请数量
     */
    @GetMapping("/adoptionStatusCount")
    public Map<String, Object> getAdoptionStatusCount() {
        Map<String, Object> res = new HashMap<>();

        // 使用 LambdaQueryChainWrapper 逐个统计状态的数量
        long pendingCount = adoptionService.lambdaQuery().eq(Adoption::getStatus, 0).count(); // 0: 待审核
        long approvedCount = adoptionService.lambdaQuery().eq(Adoption::getStatus, 1).count(); // 1: 审核通过
        long rejectedCount = adoptionService.lambdaQuery().eq(Adoption::getStatus, 2).count(); // 2: 被驳回

        Map<String, Long> statusCount = new HashMap<>();
        statusCount.put("待审核", pendingCount);
        statusCount.put("审核通过", approvedCount);
        statusCount.put("驳回", rejectedCount);

        res.put("code", 200);
        res.put("data", statusCount);
        return res;
    }

    /**
     * 按动物统计领养数量
     */
    @GetMapping("/adoptionAnimalCount")
    public Map<String, Object> getAdoptionAnimalCount() {
        Map<String, Object> res = new HashMap<>();

        // 使用 QueryWrapper 结合原生 SQL 语法
        List<Map<String, Object>> animalCount = adoptionService
                .getBaseMapper() // 获取原始 Mapper 进行操作
                .selectMaps(new QueryWrapper<Adoption>()
                        .select("animal_id", "COUNT(*) as count")  // 使用原生 SQL 查询语法
                        .groupBy("animal_id") // 按 animal_id 分组
                );

        res.put("code", 200);
        res.put("data", animalCount);
        return res;
    }



}
